// pages/myCollection/myCollection.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: [],
    isShow: '',
    choseImg: '/static/tabs/select.png',
    unchoseImg: '/static/tabs/unselect.png',
    allSelect: false,
    selectValue: '',
    collectIds: [] //	收藏表ID
  },
  addTag() { //若是从服务器取来的数据，则需增加一个变量isSelect用来判断是否选中
    for (var i = 0; i < this.data.array.length; i++) {
      this.data.array[i].isSelect = false;
    }
  },
  todeltis(e) {
    wx.navigateTo({
      url: '../details/details?cid=' + e.currentTarget.dataset.id,
    })
  },

  //单击选中或取消按钮
  chooseTap(e) {
    let index = e.currentTarget.dataset.index; //当前点击列表的index
    this.setData({
      selectValue: index
    })
    let infoArray = this.data.array;
    // console.log(infoArray)
    infoArray[index].isSelect = !infoArray[index].isSelect; //选中变为未选中，未选中变为选中
    this.checkAllSelect();
    this.setData({
      array: infoArray
    })
  },
  //全选或者取消全选
  allSelect() {
    let infoArray = this.data.array;
    let allSelect = this.data.allSelect;
    if (allSelect) {
      for (var i = 0; i < infoArray.length; i++) {
        infoArray[i].isSelect = false;
      }
    } else {
      for (var i = 0; i < infoArray.length; i++) {
        infoArray[i].isSelect = true;
      }
    }
    allSelect = !allSelect
    this.setData({
      array: infoArray,
      allSelect
    })
  },
  checkAllSelect() { //判断是否是全选
    let infoArray = this.data.array;
    let temp = []; //用一个临时数组来存放已经选中的列表的下标
    let allSelect = this.data.allSelect;
    for (var i = 0; i < infoArray.length; i++) {
      if (infoArray[i].isSelect == true) {
        temp.push(i);

      }
    }
    temp.length === infoArray.length ? allSelect = true : allSelect = false

    this.setData({
      allSelect
    })
  },
  // 控制是否显示删除按钮
  manage() {
    if (this.data.isShow == true) {
      this.setData({
        isShow: false
      })
    } else {
      this.setData({
        isShow: true
      })
    }
  },
  // 删除
  delcollection() {
    var that = this
    var value = that.data.selectValue
    var arr = that.data.array
    var allSelect = that.data.allSelect
    var collectIds = []
    //判断是否点击全选
    if (allSelect == true) { //若点击全选则清空数组
      // console.log()
      var arrIds = that.data.array
      arrIds.forEach(item => {
        collectIds.push(item.id)
      })
      collectIds = collectIds.join()
      // console.log(collectIds)
      wx.request({
        url: app.globalData.baseUrl + '/product/delCollect',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          xcxOpenId: app.globalData.xcxOpenId,
          collectIds
        },
        method: 'POST',
        success(res) {
          that.setData({
            allSelect: false
          })
          // console.log(res)
          if (res.data.code == 0) {
            that.search()
          }
        }
      })


    } else { //否则删除单个商品
      arr.splice(value, 1)
      collectIds = arr[value].id
      wx.request({
        url: app.globalData.baseUrl + '/product/delCollect',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          xcxOpenId: app.globalData.xcxOpenId,
          collectIds
        },
        method: 'POST',
        success(res) {
          if (res.data.code == 0) {
            that.search()
          }
        }
      })
      that.setData({
        array: arr
      })
    }
  },
  // 查询产品收藏列表
  search() {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/product/findAllCollect',
      data: {
        xcxOpenId: app.globalData.xcxOpenId,
      },
      success(res) {
        var data = res.data.data.rows
        data.forEach(item => {
          item.productDTO.mainImageUrl = 'https://www.banmapc.cn/wx/api/v1/getFile?id=' + parseInt(item.productDTO.mainImageUrl)
        })
        that.setData({
          array: data
        })
        // console.log(that.data.array)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.search()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.reLaunch({
      url: '../me/me',
    })
  }
})